package yunplus.io.hchome.display.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;

import yunplus.io.hchome.R;
import yunplus.io.hchome.display.video.DeviceInfo;
import yunplus.io.hchome.display.video.VideoThread;

import static android.R.attr.port;

/**
 * Created by cruze on 2017/6/7.
 */
public class VedioActivity extends BaseActivity implements View.OnClickListener {

    public static final String TAG = VedioActivity.class.getSimpleName();

    private static final int REQUEST_CODE_GET_NOTE_INFO = 0x01;
    private static final String DEVICE_INFO = "device_info";

    private int mCurrentClickButtonId = -1;

    SurfaceView mSurfaceView1;
    SurfaceView mSurfaceView2;
    SurfaceView mSurfaceView3;
    SurfaceView mSurfaceView4;

    ImageButton mIbtn1, mIbtn2, mIbtn3, mIbtn4;

    VideoThread mVt1, mVt2, mVt3, mVt4 ;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vedio);

        mVt1 = new VideoThread();
        mVt2 = new VideoThread();
        mVt3 = new VideoThread();
        mVt4 = new VideoThread();

        mSurfaceView1 = (SurfaceView) findViewById(R.id.surface_view_1);
        mSurfaceView1.getHolder().addCallback(new MySurfaceHolderCallback(mSurfaceView1, mVt1));
        mSurfaceView2 = (SurfaceView) findViewById(R.id.surface_view_2);
        mSurfaceView2.getHolder().addCallback(new MySurfaceHolderCallback(mSurfaceView2, mVt2));
        mSurfaceView3 = (SurfaceView) findViewById(R.id.surface_view_3);
        mSurfaceView3.getHolder().addCallback(new MySurfaceHolderCallback(mSurfaceView3, mVt3));
        mSurfaceView4 = (SurfaceView) findViewById(R.id.surface_view_4);
        mSurfaceView4.getHolder().addCallback(new MySurfaceHolderCallback(mSurfaceView4, mVt4));

        mIbtn1 = (ImageButton) findViewById(R.id.ibtn_1);
        mIbtn1.setOnClickListener(this);
        mIbtn2 = (ImageButton) findViewById(R.id.ibtn_2);
        mIbtn2.setOnClickListener(this);
        mIbtn3 = (ImageButton) findViewById(R.id.ibtn_3);
        mIbtn3.setOnClickListener(this);
        mIbtn4 = (ImageButton) findViewById(R.id.ibtn_4);
        mIbtn4.setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        mVt1.start();
        mVt2.start();
        mVt3.start();
        mVt4.start();
        super.onResume();

    }

    @Override
    protected void onStop() {
        super.onStop();
        mVt1.exit();
        mVt2.exit();
        mVt3.exit();
        mVt4.exit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ibtn_1:
            case R.id.ibtn_2:
            case R.id.ibtn_3:
            case R.id.ibtn_4:
                startActivityForResult(v.getId());
                break;
        }

    }

    private void startActivityForResult(int buttonId) {
        mCurrentClickButtonId = buttonId;
        startActivityForResult(
                new Intent(this, DeviceListActivity.class),
                REQUEST_CODE_GET_NOTE_INFO);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        DeviceInfo di = new DeviceInfo();
        di.setName(data.getStringExtra(DeviceInfo.NAME));
        di.setAddress(data.getStringExtra(DeviceInfo.ADDRESS));
        di.setPort(data.getIntExtra(DeviceInfo.PORT, -1));
        di.setUsername(data.getStringExtra(DeviceInfo.USERNAME));
        di.setPasswd(data.getStringExtra(DeviceInfo.PASSWD));
        VideoThread vt = null;
        switch (mCurrentClickButtonId) {
            case R.id.ibtn_1:
                vt = mVt1;
                break;
            case R.id.ibtn_2:
                vt = mVt2;
                break;
            case R.id.ibtn_3:
                vt = mVt3;
                break;
            case R.id.ibtn_4:
                vt = mVt4;
                break;
        }
        mCurrentClickButtonId = -1;
        if(null!=vt) {
            vt.setDeviceInfo(di);
            vt.startPlay();
        }
    }

    public static class MySurfaceHolderCallback implements SurfaceHolder.Callback {

        SurfaceView mmSurfaceView;
        VideoThread mmVt;

        public MySurfaceHolderCallback(SurfaceView surfaceView, VideoThread vt) {
            mmSurfaceView = surfaceView;
            mmVt = vt;
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            Log.d(TAG, "surfaceCreated---port:" + port + " mmSurfaceView");
            if(null!=mmVt) {
                mmVt.setSurface(mmSurfaceView);
            }

        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            Log.d(TAG, "surfaceChanged");
            if(null!=mmVt) {
                mmVt.changeSurface(width, height);
            }
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            Log.d(TAG, "surfaceDestroyed");
            if(null!=mmVt) {
                mmVt.stopPlay();
            }
        }
    }


}
